package com.ruoyi.market.service.impl;

import com.ruoyi.common.constant.DictConstants;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.market.domain.Banner;
import com.ruoyi.market.mapper.BannerMapper;
import com.ruoyi.market.service.IBannerService;
import com.ruoyi.system.service.ISysConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Validator;
import java.util.List;

/**
 * 轮播图 业务层处理
 * 
 * @author ruoyi
 */
@Service
public class BannerServiceImpl implements IBannerService
{
    private static final Logger log = LoggerFactory.getLogger(BannerServiceImpl.class);

    @Autowired
    private BannerMapper bannerMapper;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    protected Validator validator;

    /**
     * 根据条件分页查询轮播图列表
     * 
     * @param banner 轮播图信息
     * @return 轮播图信息集合信息
     */
    @Override
    public List<Banner> selectBannerList(Banner banner)
    {
        return bannerMapper.selectBannerList(banner);
    }

    /**
     * 通过轮播图名查询轮播图
     * 
     * @param bannerTitle 轮播图名
     * @return 轮播图对象信息
     */
    @Override
    public Banner selectBannerByBannerTitle(String bannerTitle)
    {
        return bannerMapper.selectBannerByBannerTitle(bannerTitle);
    }

    /**
     * 通过轮播图ID查询轮播图
     * 
     * @param bannerId 轮播图ID
     * @return 轮播图对象信息
     */
    @Override
    public Banner selectBannerById(Long bannerId)
    {
        return bannerMapper.selectBannerById(bannerId);
    }

    /**
     * 新增保存轮播图信息
     * 
     * @param banner 轮播图信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertBanner(Banner banner)
    {
        // 新增轮播图信息
        int rows = bannerMapper.insertBanner(banner);
        return rows;
    }

    /**
     * 修改保存轮播图信息
     * 
     * @param banner 轮播图信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateBanner(Banner banner)
    {
        return bannerMapper.updateBanner(banner);
    }

    /**
     * 修改轮播图状态
     * 
     * @param banner 轮播图信息
     * @return 结果
     */
    @Override
    public int updateBannerStatus(Banner banner)
    {
        return bannerMapper.updateBanner(banner);
    }

    /**
     * 批量删除轮播图信息
     * 
     * @param bannerIds 需要删除的轮播图ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteBannerByIds(Long[] bannerIds) { return bannerMapper.deleteBannerByIds(bannerIds); }

    /**
     * 导入轮播图数据
     * 
     * @param bannerList 轮播图数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作轮播图
     * @return 结果
     */
    @Override
    public String importBanner(List<Banner> bannerList, Boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(bannerList) || bannerList.size() == 0)
        {
            throw new ServiceException("导入轮播图数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        String password = configService.selectConfigByKey("sys.banner.initPassword");
        for (Banner banner : bannerList)
        {
            try
            {
                BeanValidators.validateWithException(validator, banner);
                banner.setCreateBy(operName);
                bannerMapper.insertBanner(banner);
                successNum++;
                successMsg.append("<br/>" + successNum + "、轮播图 " + banner.getBannerTitle() + " 导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、轮播图 " + banner.getBannerTitle() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
